package lte

type SSS struct {
	ID   int
	Solt int
	ofd  []complex128
	fd   []complex128
	tlen int
	td   []complex128
}

func (p *SSS) Fd() []complex128  { return p.fd }
func (p *SSS) Ofd() []complex128 { return p.ofd }
func (p *SSS) Td() []complex128  { return p.td }

func NewSSS(cid, slot int) *SSS {
	id_1 := cid / 3
	id_2 := cid % 3
	qp := id_1 / 30                //qp = id_1//30
	q := (id_1 + qp*(qp+1)/2) / 30 //q  = (id_1+qp*(qp+1)//2)//30
	mp := id_1 + q*(q+1)/2         //mp = id_1+q*(q+1)//2
	m0 := mp % 31                  //m0 = mp%31
	m1 := (m0 + mp/31 + 1) % 31    //m1 = (m0+mp//31+1)%31
	s_td := []int{0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1}
	c_td := []int{0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1}
	z_td := []int{0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1}
	s0_m0 := make([]int, 31)
	s1_m1 := make([]int, 31)
	c0 := make([]int, 31)
	c1 := make([]int, 31)
	z1_m0 := make([]int, 31)
	z1_m1 := make([]int, 31)
	for i := 0; i < 31; i++ {
		s0_m0[i] = 1 - 2*s_td[(i+m0)%31]   //s0_m0 = np.array([ s_td[(i+m0)%31] for i in range(31)])
		s1_m1[i] = 1 - 2*s_td[(i+m1)%31]   //s1_m1 = np.array([ s_td[(i+m1)%31] for i in range(31)])
		c0[i] = 1 - 2*c_td[(i+id_2)%31]    //c0    = np.array([ c_td[(i+id_2)%31] for i in range(31)])
		c1[i] = 1 - 2*c_td[(i+id_2+3)%31]  //c1    = np.array([ c_td[(i+id_2+3)%31] for i in range(31)])
		z1_m0[i] = 1 - 2*z_td[(i+m0%8)%31] //z1_m0 = np.array([ z_td[(i+m0%8)%31] for i in range(31)])
		z1_m1[i] = 1 - 2*z_td[(i+m1%8)%31] //z1_m1 = np.array([ z_td[(i+m1%8)%31] for i in range(31)])
	}
	ssc1 := make([]int, 31)
	ssc2 := make([]int, 31)
	for i := 0; i < 31; i++ {
		if slot == 0 {
			ssc2[i] = s1_m1[i] * c1[i] * z1_m0[i]
			ssc1[i] = s0_m0[i] * c0[i]
		} else {
			ssc2[i] = s0_m0[i] * c1[i] * z1_m1[i]
			ssc1[i] = s1_m1[i] * c0[i]
		}
	}
	ofd := make([]complex128, 62)
	for i := 0; i < 31; i++ {
		ofd[2*i] = complex(float64(ssc1[i]), 0.0)   //self.ofd[::2] = ssc1[:]
		ofd[2*i+1] = complex(float64(ssc2[i]), 0.0) //self.ofd[1::2] = ssc2[:]
	}
	fd := make([]complex128, 128)
	copy(fd[1:32], ofd[31:])
	copy(fd[97:], ofd[:31])
	tx_ifft := tx_ifft_plan[6]
	_td := tx_ifft.Buf
	copy(_td, fd)
	tx_ifft.ExecuteAndScale(1.0 / 128.)
	tlen := 137
	td := make([]complex128, tlen)
	copy(td[9:], _td[:])
	copy(td[:9], _td[len(_td)-9:])
	return &SSS{cid, slot, ofd, fd, tlen, td}
}
